package com.aps.server.controller;

import com.aps.server.entity.BasicOperation;
import com.aps.server.service.BasicOperationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 * 弃用
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */


























/**
 * 工序信息控制器
 * 提供工序信息相关的REST API接口
 */
@Slf4j
@RestController
@RequestMapping("/api/basic-operations")
@RequiredArgsConstructor
@Tag(name = "工序信息管理", description = "工序信息相关的API接口")
public class BasicOperationController {
    @Resource
    BasicOperationService basicOperationService;
    
    /**
     * 创建工序信息
     */
    @PostMapping
    @Operation(summary = "创建工序信息", description = "创建一个新的工序信息")
    public ResponseEntity<Map<String, Object>> createOperation(@RequestBody BasicOperation basicOperation) {
        try {
            boolean success = basicOperationService.createOperation(basicOperation);
            if (success) {
                return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "工序信息创建成功",
                    "operationId", basicOperation.getId()
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "工序信息创建失败"
                ));
            }
        } catch (Exception e) {
            log.error("创建工序信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "创建工序信息失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 根据ID查询工序信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询工序信息", description = "根据工序ID查询工序信息（使用配置的租户ID）")
    public ResponseEntity<BasicOperation> getOperationById(
            @Parameter(description = "工序ID") @PathVariable("id") Integer id) {
        try {
            BasicOperation operation = basicOperationService.getOperationById(id);
            if (operation != null) {
                return ResponseEntity.ok(operation);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("查询工序信息失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据ID和租户ID查询工序信息
     */
    @GetMapping("/{id}/tenant")
    @Operation(summary = "根据ID和租户ID查询工序信息", description = "根据工序ID和指定租户ID查询工序信息")
    public ResponseEntity<BasicOperation> getOperationByIdWithTenant(
            @Parameter(description = "工序ID") @PathVariable Integer id,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            BasicOperation operation = basicOperationService.getOperationById(id, tenantId);
            if (operation != null) {
                return ResponseEntity.ok(operation);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("查询工序信息失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 更新工序信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新工序信息", description = "根据工序ID更新工序信息")
    public ResponseEntity<Map<String, Object>> updateOperation(
            @Parameter(description = "工序ID") @PathVariable String id,
            @RequestBody BasicOperation basicOperation) {
        try {
            basicOperation.setId(id);
            boolean success = basicOperationService.updateOperation(basicOperation);
            if (success) {
                return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "工序信息更新成功"
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "工序信息更新失败"
                ));
            }
        } catch (Exception e) {
            log.error("更新工序信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "更新工序信息失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 删除工序信息
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除工序信息", description = "根据工序ID删除工序信息（使用配置的租户ID）")
    public ResponseEntity<Map<String, Object>> deleteOperation(
            @Parameter(description = "工序ID") @PathVariable("id") Integer id) {
        try {
            boolean success = basicOperationService.deleteOperation(id);
            if (success) {
                return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "工序信息删除成功"
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "工序信息删除失败"
                ));
            }
        } catch (Exception e) {
            log.error("删除工序信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "删除工序信息失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 根据ID和租户ID删除工序信息
     */
    @DeleteMapping("/{id}/tenant")
    @Operation(summary = "根据ID和租户ID删除工序信息", description = "根据工序ID和指定租户ID删除工序信息")
    public ResponseEntity<Map<String, Object>> deleteOperationWithTenant(
            @Parameter(description = "工序ID") @PathVariable Integer id,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            boolean success = basicOperationService.deleteOperation(id, tenantId);
            if (success) {
                return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "工序信息删除成功"
                ));
            } else {
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "工序信息删除失败"
                ));
            }
        } catch (Exception e) {
            log.error("删除工序信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "删除工序信息失败: " + e.getMessage()
            ));
        }
    }
    
    /**
     * 查询当前租户的工序列表
     */
    @GetMapping("/current-tenant")
    @Operation(summary = "查询当前租户的工序列表", description = "根据配置的租户ID查询所有工序")
    public ResponseEntity<List<BasicOperation>> getOperationsByCurrentTenant() {
        try {
            List<BasicOperation> operations = basicOperationService.getOperationsByTenantId();
            return ResponseEntity.ok(operations);
        } catch (Exception e) {
            log.error("根据当前租户ID查询工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据租户ID查询工序列表
     */
    @GetMapping("/tenant/{tenantId}")
    @Operation(summary = "根据租户ID查询工序列表", description = "根据指定租户ID查询所有工序")
    public ResponseEntity<List<BasicOperation>> getOperationsByTenantId(
            @Parameter(description = "租户ID") @PathVariable("tenantId") String tenantId) {
        try {
            List<BasicOperation> operations = basicOperationService.getOperationsByTenantId(tenantId);
            return ResponseEntity.ok(operations);
        } catch (Exception e) {
            log.error("根据租户ID查询工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 分页查询当前租户的工序列表
     */
    @GetMapping("/current-tenant/page")
    @Operation(summary = "分页查询当前租户的工序列表", description = "根据配置的租户ID分页查询工序列表")
    public ResponseEntity<IPage<BasicOperation>> getOperationsByCurrentTenantPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        try {
            IPage<BasicOperation> page = basicOperationService.getOperationsByTenantIdPage(current, size);
            return ResponseEntity.ok(page);
        } catch (Exception e) {
            log.error("分页查询当前租户工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据租户ID分页查询工序列表
     */
    @GetMapping("/tenant/{tenantId}/page")
    @Operation(summary = "分页查询工序列表", description = "根据指定租户ID分页查询工序列表")
    public ResponseEntity<IPage<BasicOperation>> getOperationsByTenantIdPage(
            @Parameter(description = "租户ID") @PathVariable String tenantId,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        try {
            IPage<BasicOperation> page = basicOperationService.getOperationsByTenantIdPage(current, size, tenantId);
            return ResponseEntity.ok(page);
        } catch (Exception e) {
            log.error("分页查询工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据工艺编码查询工序列表
     */
    @GetMapping("/craft/{craftId}")
    @Operation(summary = "根据工艺编码查询工序列表", description = "根据工艺编码和租户ID查询工序列表")
    public ResponseEntity<List<BasicOperation>> getOperationsByCraftId(
            @Parameter(description = "工艺编码") @PathVariable String craftId,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            List<BasicOperation> operations = basicOperationService.getOperationsByCraftId(craftId, tenantId);
            return ResponseEntity.ok(operations);
        } catch (Exception e) {
            log.error("根据工艺编码查询工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据工艺编码分页查询工序列表
     */
    @GetMapping("/craft/{craftId}/page")
    @Operation(summary = "根据工艺编码分页查询工序列表", description = "根据工艺编码和租户ID分页查询工序列表")
    public ResponseEntity<IPage<BasicOperation>> getOperationsByCraftIdPage(
            @Parameter(description = "工艺编码") @PathVariable String craftId,
            @Parameter(description = "租户ID") @RequestParam String tenantId,
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {
        try {
            IPage<BasicOperation> page = basicOperationService.getOperationsByCraftIdPage(current, size, craftId, tenantId);
            return ResponseEntity.ok(page);
        } catch (Exception e) {
            log.error("根据工艺编码分页查询工序失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据工序名称查询工序信息
     */
    @GetMapping("/operation-name/{operationName}")
    @Operation(summary = "根据工序名称查询工序信息", description = "根据工序名称和租户ID查询工序信息")
    public ResponseEntity<BasicOperation> getOperationByOperationName(
            @Parameter(description = "工序名称") @PathVariable String operationName,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            BasicOperation operation = basicOperationService.getOperationByOperationName(operationName, tenantId);
            if (operation != null) {
                return ResponseEntity.ok(operation);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("根据工序名称查询工序信息失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据工序类型查询工序列表
     */
    @GetMapping("/operation-type/{operationType}")
    @Operation(summary = "根据工序类型查询工序列表", description = "根据工序类型和租户ID查询工序列表")
    public ResponseEntity<List<BasicOperation>> getOperationsByOperationType(
            @Parameter(description = "工序类型") @PathVariable String operationType,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            List<BasicOperation> operations = basicOperationService.getOperationsByOperationType(operationType, tenantId);
            return ResponseEntity.ok(operations);
        } catch (Exception e) {
            log.error("根据工序类型查询工序列表失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 根据设备类型要求查询工序列表
     */
    @GetMapping("/equipment-type/{equipmentTypeRequire}")
    @Operation(summary = "根据设备类型要求查询工序列表", description = "根据设备类型要求和租户ID查询工序列表")
    public ResponseEntity<List<BasicOperation>> getOperationsByEquipmentTypeRequire(
            @Parameter(description = "设备类型要求") @PathVariable String equipmentTypeRequire,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            List<BasicOperation> operations = basicOperationService.getOperationsByEquipmentTypeRequire(equipmentTypeRequire, tenantId);
            return ResponseEntity.ok(operations);
        } catch (Exception e) {
            log.error("根据设备类型要求查询工序列表失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 统计租户下的工序数量
     */
    @GetMapping("/count/tenant/{tenantId}")
    @Operation(summary = "统计租户工序数量", description = "统计指定租户下的工序数量")
    public ResponseEntity<Long> countOperationsByTenantId(
            @Parameter(description = "租户ID") @PathVariable("tenantId") String tenantId) {
        try {
            Long count = basicOperationService.countOperationsByTenantId(tenantId);
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            log.error("统计租户工序数量失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 统计工艺编码下的工序数量
     */
    @GetMapping("/count/craft/{craftId}")
    @Operation(summary = "统计工艺工序数量", description = "统计指定工艺编码下的工序数量")
    public ResponseEntity<Long> countOperationsByCraftId(
            @Parameter(description = "工艺编码") @PathVariable String craftId,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            Long count = basicOperationService.countOperationsByCraftId(craftId, tenantId);
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            log.error("统计工艺工序数量失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 统计租户下指定类型的工序数量
     */
    @GetMapping("/count/operation-type/{operationType}")
    @Operation(summary = "统计指定类型工序数量", description = "统计指定租户下指定类型的工序数量")
    public ResponseEntity<Long> countOperationsByOperationType(
            @Parameter(description = "工序类型") @PathVariable String operationType,
            @Parameter(description = "租户ID") @RequestParam String tenantId) {
        try {
            Long count = basicOperationService.countOperationsByOperationType(operationType, tenantId);
            return ResponseEntity.ok(count);
        } catch (Exception e) {
            log.error("统计指定类型工序数量失败", e);
            return ResponseEntity.internalServerError().build();
        }
    }
    
    /**
     * 批量创建工序信息
     */
    @PostMapping("/batch")
    @Operation(summary = "批量创建工序信息", description = "批量创建多个工序信息")
    public ResponseEntity<Map<String, Object>> batchCreateOperations(@RequestBody List<BasicOperation> operations) {
        try {
            int successCount = basicOperationService.batchCreateOperations(operations);
            return ResponseEntity.ok(Map.of(
                "success", true,
                "message", "批量创建工序信息完成",
                "totalCount", operations.size(),
                "successCount", successCount,
                "failureCount", operations.size() - successCount
            ));
        } catch (Exception e) {
            log.error("批量创建工序信息失败", e);
            return ResponseEntity.internalServerError().body(Map.of(
                "success", false,
                "message", "批量创建工序信息失败: " + e.getMessage()
            ));
        }
    }
}
